package dao

import (
	"flamego-study/model"

	"github.com/charmbracelet/log"
)

type userDaoImpl struct {
}

func (userDaoImpl) ListAll() []model.UserModel {
	var userModels []model.UserModel
	engine.Find(&userModels)
	return userModels
}

func (userDaoImpl) Save(userModel model.UserModel) int64 {
	one, err := engine.InsertOne(&userModel)
	if err != nil {
		log.Error(err)
	}
	return one
}

func (userDaoImpl) Delete(userModel model.UserModel) int64 {
	one, err := engine.Delete(&userModel)
	if err != nil {
		log.Error(err)
	}
	return one
}

// 空值不更新？
func (userDaoImpl) Update(userModel model.UserModel) int64 {

	// 空值不更新！确实很不错。
	one, err := engine.Update(&userModel, model.UserModel{Name: userModel.Name})
	if err != nil {
		log.Error(err)
	}
	return one
}

var UserDao = new(userDaoImpl)
